package com.browan.constellation;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ProgressBar;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import com.browan.constellation.data.RSSData;
import com.browan.constellation.db.DbHelper;

public class HistoryActivity extends ListActivity
{
	private ProgressBar m_progressBar;

	private TextView tv;


	private Cursor cursor;

	private DbHelper m_dbHelper;


	private SimpleCursorAdapter adapter;


	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rss_main);

		m_progressBar = (ProgressBar) findViewById(R.id.progressbar);
		m_progressBar.setVisibility(View.GONE);

		tv = (TextView) findViewById(android.R.id.empty);
		tv.setGravity(Gravity.CENTER);
		tv.setText("没有收藏数据！");

		getListView().setOnCreateContextMenuListener(this);


		m_dbHelper = new DbHelper(this);


		cursor = m_dbHelper.getHistoryCurson();

		adapter = new SimpleCursorAdapter(this, R.layout.rss_item, cursor, new String[] { RSSData.ITEM_TITLE,
				RSSData.ITEM_DESC, RSSData.ITEM_PUBDATE }, new int[] { R.id.title, R.id.content, R.id.pub_date });
		setListAdapter(adapter);
	}


	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
	{
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("操作");
		menu.add(0, Menu.FIRST, 0, "分享");
		menu.add(0, Menu.FIRST + 1, 0, "删除");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		int selectedPosition = ((AdapterContextMenuInfo) item.getMenuInfo()).position;
		Cursor cursor = (Cursor) adapter.getItem(selectedPosition);

		boolean ret = true;

		switch (item.getItemId())
		{
		case Menu.FIRST:
			Intent intent = new Intent(Intent.ACTION_SEND);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_SUBJECT, cursor.getString(cursor.getColumnIndexOrThrow(RSSData.ITEM_TITLE)));
			intent.putExtra(Intent.EXTRA_TEXT, cursor.getString(cursor.getColumnIndexOrThrow(RSSData.ITEM_DESC))
					+ "\n\n" + cursor.getString(cursor.getColumnIndexOrThrow(RSSData.ITEM_LINK)));
			startActivity(Intent.createChooser(intent, getTitle()));

			break;

		case Menu.FIRST + 1:
			m_dbHelper.unHistory(new RSSData(cursor));
			cursor.requery();
			adapter.notifyDataSetChanged();
			break;

		default:
			ret = super.onContextItemSelected(item);
			break;
		}
		return ret;
	}

	protected void onResume()
	{
		cursor.requery();
		adapter.notifyDataSetChanged();
		super.onResume();
	};

	protected void onListItemClick(android.widget.ListView l, android.view.View v, int position, long id)
	{

	

		Cursor cursor = (Cursor) adapter.getItem(position);

	

		String title = cursor.getString(cursor.getColumnIndexOrThrow(RSSData.ITEM_TITLE));

		String message = cursor.getString(cursor.getColumnIndexOrThrow(RSSData.ITEM_DESC));

		final String link = cursor.getString(cursor.getColumnIndexOrThrow(RSSData.ITEM_LINK));

		if (TextUtils.isEmpty(message))
		{
			startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link)));
		}
		else
		{
			AlertDialog dialog = new AlertDialog.Builder(this).setTitle(title).setMessage(message).setPositiveButton(
					"查看", new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link)));
						}
					}).setNegativeButton("取消", null).create();
			dialog.show();
		}
	
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			exitDialog();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	protected boolean exitDialog()
	{
		new AlertDialog.Builder(this).setTitle("退出").setMessage("确定退出程序？").setPositiveButton("是", new OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				HistoryActivity.this.finish();
			}
		}).setNegativeButton("否", null).setCancelable(false).create().show();
		return true;
	};

	@Override
	protected void onPause()
	{
		super.onPause();
	}

	@Override
	protected void onDestroy()
	{
		if (null != cursor)
		{
			cursor.close();
			cursor = null;
		}
		m_dbHelper.close();

		super.onDestroy();
	}

}